package main

import (
	"fmt"
	"strings"
	"encoding/csv"
	"os"
	"github.com/gocolly/colly"
)


func getStockListA(saveFile string) (err error){
	stocks, err := getStockList("http://query.sse.com.cn/security/stock/downloadStockListFile.do?csrcCode=&stockCode=&areaName=&stockType=1")
	if err != nil {
		return err
	}
	err = saveStockList2CSV(stocks,saveFile)
	return
}

func saveStockList2CSV(stockList string,file string)(err error){



	vals := strings.Split(stockList,"\n")

	f,err := os.Create(file)
	if err != nil{
		return err
	}
	defer f.Close()
	fw := csv.NewWriter(f)

	for _ ,row := range vals{
		rSplits := strings.Split(row,"\t")

		rSplitsRslt := make([]string,0)
		for _,sp := range rSplits{
			trimSp := strings.Trim(sp," ")
			if len(trimSp) > 0{
				rSplitsRslt = append(rSplitsRslt,trimSp)
			}
		}

		if len(rSplitsRslt) > 0{
			err = fw.Write(rSplitsRslt)
			if err != nil {
				return err
			}
		}
	}
	fw.Flush()
	return
}

//传下载地址 返回  列表
func getStockList(url string)(stockList string,err error){
	//http 头协议
	//GET http://query.sse.com.cn/security/stock/downloadStockListFile.do?csrcCode=&stockCode=&areaName=&stockType=1 HTTP/1.1
	//Host: query.sse.com.cn
	//Connection: keep-alive	
	//Accept: */*
	//Origin: http://www.sse.com.cn      当前请求来自于哪个站点 （不包含任何路径信息）
	//User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36
	//Referer: http://www.sse.com.cn/assortment/stock/list/share/  表明从哪个网页调过来的
	//Accept-Encoding: gzip, deflate    gzip压缩类型   defalte压缩算法
	//Accept-Language: zh-CN,zh;q=0.9`


	c := colly.NewCollector()

	c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36"
	c.OnRequest(func(r *colly.Request){
		r.Headers.Set("Host", "query.sse.com.cn")
        r.Headers.Set("Connection", "keep-alive")
        r.Headers.Set("Accept", "*/*")
        r.Headers.Set("Origin", "http://www.sse.com.cn")
        r.Headers.Set("Referer", "http://www.sse.com.cn/assortment/stock/list/share/") //关键头 如果没有 则返回 错误
        r.Headers.Set("Accept-Encoding", "gzip, deflate")
        r.Headers.Set("Accept-Language", "zh-CN,zh;q=0.9")
	})
	c.OnResponse(func(resp *colly.Response){
		stockList = string(resp.Body)
		fmt.Printf("%T \n %v",stockList,stockList)
	})

	c.OnError(func(resp *colly.Response,errHttp error){
		err = errHttp
	})

	err = c.Visit(url)
	return 

}



func main() {
    
    // var err error
    // err = 
    getStockListA("e:\\sseA.csv")
    // if err != nil {
    	// fmt.Println(err)
    // }
}